function addAdContent(id) {
    if ($("#ad_content_"+id+" .adv-col-5").length == 0) {
        length = 1;
    }else{
        var length = $("#ad_content_"+id+" .sort-list-col:last").data('key') + 1;
    }
    
    var html = "<div class=\"adv-col-5 sort-list-col\" id='ad_content_div_"+id+"_"+length+"' data-key=\""+length+"\">\n" +
        "                            <div class=\"adv-etm\">\n" +
        "                                <div class=\"adv-etm-l\">\n" +
        "                                    <input type=\"hidden\" id='input_content_images_"+id+"_"+length+"' name='data["+length+"][content_images]' value=''>\n" +
        "                                    <img id='img_content_images_"+id+"_"+length+"' src=\"/public/static/common/images/default_litpic_x.png\">\n" +
        "                                    <span class=\"s-tihuan\"><i class=\"iconfont e-bianji2 f-24\" onclick=\"GetUploadify(1,'','allimg','ci_call_back_"+id+"_"+length+"');\"></i></span>\n" +
        "                                </div>\n" +
        "                                <div class=\"adv-etm-r\">\n" +
        // "                                    <div class=\"title\">\n" +
        // "                                        <input name=\"data["+length+"][content_title]\" placeholder=\"\" class=\"input-txt\" type=\"text\" onchange=\"changeAdTitle(this,"+id+");\" />\n" +
        // "                                    </div>\n" +
        "                                    <div class=\"set-bt\">\n" +
        "                                        <input type=\"hidden\" name=\"data["+length+"][link_id]\" value=\"1\" id='linkIDHidden_"+id+"_"+length+"'>\n" +
        "                                        <input type=\"hidden\" name='data["+length+"][host_id]' value=\"1\" id='hostIDHidden_"+id+"_"+length+"'>\n" +
        "                                        <span id=\"ad_link_title_"+id+"_"+length+"\" title=\"\"></span>\n" +
        "                                        <a href=\"javascript:void(0);\" data-ad_host_id=\"1\" data-ad_link_id=\"1\" onclick=\"adSelectLink(this,"+id+","+length+");\">设置链接</a>\n" +
        "                                    </div>\n" +
        "                                </div>\n" +
        "                                <span class=\"delect\" data-id='"+id+"_"+length+"' data-url='' onClick=\"del_ad_0429(this);\"></span>\n" +
        "                                <span class=\"drag-handle\"><i class=\"iconfont e-gengduo\"></i></span>\n" +
        "                            </div>\n" +
        "                        </div>" +
        "<script type=\"text/javascript\">" +
        "        function ci_call_back_"+id+"_"+length+"(fileurl_tmp){" +
        "            call_back('"+id+"_"+length+"','content_images',fileurl_tmp,"+id+");" +
        "        } " +
        "</script> ";
    $("#ad_content_"+id+" .adv-col-5:last").before(html);

}

// 选择链接
function adSelectLink(obj, id,value) {
    if (value && parseInt(value) > 0) {
        $(obj).data('ad_host_id', $('#hostIDHidden_'+id+'_' + value).val());
        $(obj).data('ad_link_id', $('#linkIDHidden_'+id+'_' + value).val());
        // 打开选择链接
        selectAppletsAdLink(obj, function(result) {
            $('#hostIDHidden_'+id+'_' + value).val(result.host_id);
            $('#linkIDHidden_'+id+'_' + value).val(result.link_id);
            $('#ad_link_title_'+id+'_' + value).text(result.link_names);
            submit_ad(id);
        });
    } else {
        showErrorAlert('数据异常，刷新重试');
    }
}

function changeAdTitle(obj,id) {
    submit_ad(id);
}

function submit_ad(id){
    var post_url = $("#post_ad_content_"+id).data('url');
    // layer_loading('正在处理');
    $.ajax({
        type: 'POST',
        url: post_url,
        data: $("#post_ad_content_"+id).serialize(),
        dataType: 'json',
        success: function(res) {
            // layer.closeAll();
            if (res.code == 1) {
                layer.msg(res.msg, {icon: 1, time:1000}, function(){
                    location.reload();
                });
            } else {
                layer.msg(res.msg, {icon: 5});
            }
        },
        error:function (e) {
            layer.alert(e.responseText);
        }
    });
}
function del_ad_0429(obj) {
    // 删除按钮
    layer.confirm('确认删除？', {
        area: ['480px', '190px'],
        move: false,
        title: '提示',
        shade: layer_shade,
        btnAlign:'r',
        closeBtn: 3,
        btn: ['确定','取消'] ,//按钮
        success: function () {
              $(".layui-layer-content").css('text-align', 'left');
          }
    }, function () {
        //确认删除后续操作
        var url = $(obj).data('url');
        if (url){
            $.ajax({
                type: 'POST',
                url: url,
                data: {_ajax:1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (res.code != 1) {
                        layer.msg(res.msg, {icon: 5});
                    }else{
                        location.reload();
                    }
                },
                error:function (e) {
                    layer.alert(e.responseText);
                }
            });
        }else {
            layer.closeAll();
            var id = $(obj).data('id');
            $("#ad_content_div_"+id).eq(0).remove();
        }
    }, function (index) {
        //确认取消
        layer.closeAll(index);
    });
}


//新增底部导航
function showCustomNav() {
    if (0 == $("#bottom_post .nav-div-0429").length){
        length = 1;
    } else{
        var length = $("#bottom_post .nav-div-0429").length;
        if (6 <= length) {
            return false;
        }
        length =$("#bottom_post .nav-div-0429:last").data('key') + 1;
    }

    var html = "<div class=\"b-nav-col-5 nav-div-0429\" id='bottom_div_"+length+"' data-key='"+length+"'>\n" +
        "                                    <div class=\"b-nav-etm\">\n" +
        "                                        <div class=\"nav-litpic-box\">\n" +
        "                                            <div class=\"box-l\">图标</div>\n" +
        "                                            <div class=\"box-r\">\n" +
        "                                                <div class=\"nav-litpic\">\n" +
        "                                                    <div class=\"litpic\">\n" +
        "                                                        <input type='hidden' id='input_default_pic_"+length+"' name='data["+length+"][default_pic]' value=''>" +
        "                                                        <img id=\"img_default_pic_"+length+"\" src=\"./public/static/common/images/default_litpic_x.png\">\n" +
        "                                                        <span class=\"s-tihuan\"><i class=\"iconfont e-bianji2 f-24\" onclick=\"GetUploadify(1,'','allimg','default_pic_call_back_"+length+"');\"></i></span>\n" +
        "                                                    </div>\n" +
        "                                                    <div class=\"name\">未选中</div>\n" +
        "                                                </div>\n" +
        "                                                <div class=\"nav-litpic\">\n" +
        "                                                    <div class=\"litpic\">\n" +
        "                                                        <input type='hidden' id='input_default_pic_"+length+"' name='data["+length+"][selected_pic]' value=''>" +
        "                                                        <img id=\"img_selected_pic_"+length+"\" src=\"./public/static/common/images/default_litpic_x.png\">\n" +
        "                                                        <span class=\"s-tihuan\"><i class=\"iconfont e-bianji2 f-24\" onclick=\"GetUploadify(1,'','allimg','selected_pic_call_back_"+length+"')\"></i></span>\n" +
        "                                                    </div>\n" +
        "                                                    <div class=\"name\">已选中</div>\n" +
        "                                                </div>\n" +
        "                                            </div>\n" +
        "                                        </div>\n" +
        "                                        <div class=\"title\">\n" +
        "                                            <div class=\"box-l\">标题</div>\n" +
        "                                            <div class=\"box-r\">\n" +
        "                                                <input type=\"text\" value=\"\" placeholder=\"\" onchange='changeBottomTitle(this);' class=\"input-txt\" name='data["+length+"][nav_title]'>\n" +
        "                                            </div>\n" +
        "                                        </div>\n" +
        "                                        <div class=\"set-bt\">\n" +
        "                                            <div class=\"box-l\">链接</div>\n" +
        "                                            <div class=\"box-r\">\n" +
        "                                                <input type=\"hidden\" value=\"\" name='data["+length+"][host_id]' id=\"hostIDHidden_"+length+"\">\n" +
        "                                                <input type=\"hidden\" value=\"\" name='data["+length+"][link_id]' id=\"linkIDHidden_"+length+"\">\n" +
        "                                                <span id=\"linkNames_"+length+"\"></span>\n" +
        "                                                <a href=\"javascript:void(0);\" onclick=\"bottomSelectLink(this, "+length+");\">设置链接</a>\n" +
        "                                            </div>\n" +
        "                                        </div>\n" +
        "                                    </div>\n" +
        "                                    <span class=\"delect\" data-url=\"\" data-id='"+length+"' onClick=\"del_bottom_0429(this);\"></span>\n" +
        "                                    <span class=\"drag-handle\"><i class=\"iconfont e-gengduo\"></i></span>\n" +
        "                                </div>\n" +
        "                            </div>" +
        "<script type=\"text/javascript\">" +
        "        function default_pic_call_back_"+length+"(fileurl_tmp){" +
        "            call_back("+length+",'default_pic',fileurl_tmp);" +
        "        } " +
        "        function selected_pic_call_back_"+length+"(fileurl_tmp){" +
        "            call_back("+length+",'selected_pic',fileurl_tmp);" +
        "        } " +
        "</script> ";
    $("#bottom_post .b-nav-col-5:last").before(html);
    if (length == 6){
        $("#bottom_post .b-nav-col-5:last").hide();
    }
}
function changeBottomTitle(obj) {
    submit_bottom();
}

// 选择链接
function bottomSelectLink(obj, value) {
    if (value && parseInt(value) > 0) {
        $(obj).data('ad_host_id', $('#hostIDHidden_' + value).val());
        $(obj).data('ad_link_id', $('#linkIDHidden_' + value).val());
        // 打开选择链接
        selectAppletsAdLink(obj, function(result) {
            $('#hostIDHidden_' + value).val(result.host_id);
            $('#linkIDHidden_' + value).val(result.link_id);
            $('#linkNames_' + value).text(result.link_names);
            submit_bottom();
        });
    } else {
        showErrorAlert('数据异常，刷新重试');
    }
}

//图片回调
function call_back(n,name,fileurl_tmp,id)
{
    $("#img_"+name+"_"+n).attr('src',fileurl_tmp);
    $("#input_"+name+"_"+n).val(fileurl_tmp);
    if (0 < id) {
        submit_ad(id);
    }else{
        submit_bottom();
    }
}

function submit_bottom(){
    var post_url = $("#bottom_post").data('url');
    // layer_loading('正在处理');
    $.ajax({
        type: 'POST',
        url: post_url,
        data: $("#bottom_post").serialize(),
        dataType: 'json',
        success: function(res) {
            layer.closeAll();
            if (res.code == 1) {
                layer.msg(res.msg, {icon: 1, time:1000}, function(){
                    location.reload();
                });
            } else {
                layer.msg(res.msg, {icon: 5});
            }
        },
        error:function (e) {
            layer.alert(e.responseText);
        }
    });
}

function del_bottom_0429(obj) {
    // 删除按钮
    layer.confirm('确认删除？', {
        area: ['480px', '190px'],
        move: false,
        title: '提示',
        shade: layer_shade,
        btnAlign:'r',
        closeBtn: 3,
        btn: ['确定','取消'] ,//按钮
        success: function () {
              $(".layui-layer-content").css('text-align', 'left');
          }
    }, function () {
        //确认删除后续操作
        var url = $(obj).data('url');
        var id = $(obj).data('id');
        if (url){
            layer_loading('正在处理');
            $.ajax({
                type: 'POST',
                url: url,
                data: {_ajax:1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (res.code != 1) {
                        layer.msg(res.msg, {icon: 5});
                    }else{
                        location.reload();
                    }
                },
                error:function (e) {
                    layer.alert(e.responseText);
                }
            });
        }else{
            layer.closeAll();
            $("#bottom_div_"+id).remove();
        }

    }, function (index) {
        //确认取消
        layer.closeAll(index);
    });
}


// 选择链接
function selectAppletsAdLink(obj, func_callback) {
    var ad_host_id = $(obj).data('ad_host_id') || 1;
    var ad_link_id = $(obj).data('ad_link_id') || 0;
    var url = eyou_basefile + "?m=admin&c=Decoration&a=link";
    if (parseInt(ad_host_id) > 0) url += "&ad_host_id=" + parseInt(ad_host_id);
    if (parseInt(ad_link_id) > 0) url += "&ad_link_id=" + parseInt(ad_link_id);
    layer.open({
        id: 'manual_select_course',
        type: 2,
        title: '选择链接',
        fixed: true,
        shadeClose: false,
        shade: layer_shade,
        maxmin: false,
        btn: ['确定', '关闭'],
        area: ['1200px', '80%'],
        closeBtn: 3,
        content: url,
        yes: function(index, layero) {
            // 选择的链接信息
            var body = layer.getChildFrame('body', index);
            var link_id = body.find('#link_id').val();
            var link_names = body.find('#link_names').val();
            var link_jsondata = body.find('#link_jsondata').val();
            link_jsondata = $.parseJSON(link_jsondata);
            if (parseInt(link_id) > 0) {
                link_jsondata.link_id = link_id;
                link_jsondata.link_names = link_names;
                func_callback(link_jsondata);
                layer.closeAll();
            }
        }
    });
}